package com.developer.platform.domain.listener;

import com.developer.platform.domain.event.UserEvent;
import com.developer.platform.domain.event.UserEventPublisher;
import com.developer.platform.domain.event.UserRoleEvent;
import com.developer.platform.domain.service.IAuthenticationSource;
import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;

/**
 * 用户异步事件监听
 *
 * @author ：qiaoliang
 * @date ：2020-11-04
 */
@Component
public class UserChangeEventListener {

    private final IAuthenticationSource authenticationSource;

    public UserChangeEventListener(UserEventPublisher userEventPublisher,
                                   IAuthenticationSource authenticationSource) {
        this.authenticationSource = authenticationSource;
        userEventPublisher.register(this);
    }

    @Subscribe
    public void asyncPublishUserEnabled(UserEvent userEvent){
        //  状态变更，如用户在线则进行踢出处理
        authenticationSource.kickOut(userEvent.getId());
    }

    @Subscribe
    public void asyncPublishUserEnabled(UserRoleEvent userEvent){
        //  角色变更更后，删除当前用户权限信息
        authenticationSource.removeResources(userEvent.getId());
    }

}
